package fr.unice.server;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import fr.unice.client.Playlist;
import fr.unice.client.PlaylistDTO;
import fr.unice.client.services.PlaylistService;

/**
 * The server side implementation of the Playlist RPC service.
 * 
 * @author PELLEGRINO Laurent
 * @author TROVATO Alexandre
 * 
 * @version 1.0
 */
@SuppressWarnings("serial")
public class PlaylistServiceImpl extends RemoteServiceServlet implements
		PlaylistService {

	/**
	 * {@inheritDoc}
	 */
	public Long addPlaylist(String name) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		Playlist p = new Playlist(name);

		try {
			tx.begin();

			pm.makePersistent(p);
			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}

		return this.getPlaylistByName(name).getId();
	}

	/**
	 * {@inheritDoc}
	 */
	public List<PlaylistDTO> getPlaylists() {
		Collection<Playlist> playlists = null;
		PersistenceManager pm = PMF.getPersistenceManager();
		try {
			Query q = pm.newQuery(Playlist.class);
			q.setOrdering("name");
			playlists = (List<Playlist>) q.execute();
			playlists = pm.detachCopyAll(playlists);
		} finally {
			pm.close();
		}

		ArrayList<PlaylistDTO> list = new ArrayList<PlaylistDTO>();
		for (Playlist p : playlists) {
			list.add(new PlaylistDTO(p.getId(), p.getName(), p
					.getNbAudioFiles(), p.getAudioFileIDs()));
		}

		return list;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public PlaylistDTO getPlaylistByName(String name) {
		Collection<Playlist> playlists = null;
		PersistenceManager pm = PMF.getPersistenceManager();
		Query query = pm.newQuery(Playlist.class);
		query.setFilter("name == nameParam");
		query.declareParameters("String nameParam");

		try {
			playlists = (List<Playlist>) query.execute(name);
			playlists = pm.detachCopyAll(playlists);
		} finally {
			query.closeAll();
		}

		if (playlists.size() == 0) {
			return null;
		}

		Playlist p = playlists.iterator().next();

		return new PlaylistDTO(p.getId(), p.getName(), p.getNbAudioFiles(), p
				.getAudioFileIDs());
	}

	/**
	 * {@inheritDoc}
	 */
	public void removePlaylist(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();

		try {
			tx.begin();

			Playlist p = pm.getObjectById(Playlist.class, id);
			pm.deletePersistent(p);

			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void updatePlaylist(Long id, String newName) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();

		try {
			tx.begin();

			Playlist p = pm.getObjectById(Playlist.class, id);
			p.setName(newName);
			pm.makePersistent(p);

			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void addAudioFileToPlaylist(Long playlistID, String audioFileID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {

			Playlist p = pm.getObjectById(Playlist.class, playlistID);
			if (p == null) {
				throw new RuntimeException("PFFF");
			}
			p.add(audioFileID);
			pm.makePersistent(p);

		} finally {

			pm.close();
		}
	}
}
